package org.pitest.ant;

import org.apache.tools.ant.types.Path;
import org.pitest.ant.util.PathUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Aidan Morgan
 */
public class Classpath {
  private static Logger s_log = Logger.getLogger(Classpath.class.getName());

  private PitestAntTask task;

  private List<Path> paths;

  public Classpath(PitestAntTask pitestAntTask) {
    this.task = pitestAntTask;
    this.paths = new ArrayList<Path>();
  }

  public Path getPath() {
    Path root = new Path(task.getProject());

    for (Path p : paths) {
      root.add(p);
    }

    return root;
  }

  public Path createPath() {
    Path p = new Path(task.getProject());
    paths.add(p);

    return p;
  }

  public List<String> asList() {
    List<String> result = new ArrayList<String>();

    for (File f : PathUtils.convertPath(getPath())) {
      try {
        result.add(f.getCanonicalPath());
      } catch (IOException e) {
        s_log.log(Level.WARNING, "Could not add Classpath element " + f.getAbsolutePath() + " to classpath.", e);
      }
    }

    return result;
  }

  public int size() {
    return paths.size();
  }
}
